namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class IndicatorTripleExponentiallySmoothedMovingAverage : BaseIndicator
    {
        private string firstEmaField = ("TRIX.FirstEmaField." + Common.GetGuid());
        private string secondEmaField = ("TRIX.SecondEmaField" + Common.GetGuid());
        private string thirdEmaField = ("TRIX.ThirdEmaField" + Common.GetGuid());
        private LineShape tlsTrix;
        private LineShape tlsTrma;

        public IndicatorTripleExponentiallySmoothedMovingAverage()
        {
            base.IndType = IndicatorType.TRIX;
            base.IndName = "TRIX";
            base.IndDesc = "三重指数平滑移动平均";
            base.IndPeriod.Add(new IndicatorPeriod(0, "N", 12.0));
            base.IndPeriod.Add(new IndicatorPeriod(1, "M", 12.0));
            base.SourceDic["Close"] = "";
            base.fields = new string[] { "TRIX", "TRMA" };
        }

        public override bool Contains(object obj)
        {
            if ((obj != this.TlsTrix) && (obj != this.TlsTrma))
            {
                return false;
            }
            return true;
        }

        public override double[] Get(int r)
        {
            double curValue = Convert.ToDouble(base.Crab.DataSource.Rows[r][base.SourceDic["Close"]]);
            double num2 = Common.CalculateExponentialMovingAvg(this.firstEmaField, base.SourceDic["Close"], (int) base.IndPeriod[0].ParamValue, curValue, base.Crab.DataSource, r);
            double num3 = Common.CalculateExponentialMovingAvg(this.secondEmaField, this.firstEmaField, (int) base.IndPeriod[0].ParamValue, num2, base.Crab.DataSource, r);
            double num4 = Common.CalculateExponentialMovingAvg(this.thirdEmaField, this.secondEmaField, (int) base.IndPeriod[0].ParamValue, num3, base.Crab.DataSource, r);
            double num5 = 0.0;
            if (r > 0)
            {
                double num6 = Convert.ToDouble(base.Crab.DataSource.Rows[r - 1][this.thirdEmaField]);
                if (num6 != 0.0)
                {
                    num5 = ((num4 - num6) / num6) * 100.0;
                }
            }
            double num7 = Common.CalcuteMovingAvg(r, (int) base.IndPeriod[1].ParamValue, this.tlsTrma.Name, this.tlsTrix.Name, num5, base.Crab.DataSource);
            return new double[] { num2, num3, num4, num5, num7 };
        }

        public override List<BaseShape> GetShapeList()
        {
            return new List<BaseShape> { this.tlsTrix, this.tlsTrma };
        }

        public override void Remove()
        {
            base.Crab.DeleteShape(this.TlsTrix);
            base.Crab.DeleteShape(this.TlsTrma);
            base.Crab.DataSource.Columns.Remove(this.FirstEmaField);
            base.Crab.DataSource.Columns.Remove(this.SecondEmaField);
            base.Crab.DataSource.Columns.Remove(this.ThirdEmaField);
        }

        public override bool Set(int r, string target)
        {
            try
            {
                DataRow row = base.Crab.DataSource.Rows[r];
                foreach (string str in base.SourceDic.Values)
                {
                    if ((str == target) || (target == null))
                    {
                        foreach (string str2 in base.SourceDic.Values)
                        {
                            if (row[str2] is DBNull)
                            {
                                return false;
                            }
                        }
                        double[] numArray = this.Get(r);
                        base.Crab.SetValue(this.FirstEmaField, numArray[0], r);
                        base.Crab.SetValue(this.SecondEmaField, numArray[1], r);
                        base.Crab.SetValue(this.ThirdEmaField, numArray[2], r);
                        base.Crab.SetValue(this.TlsTrix.Name, numArray[3], r);
                        base.Crab.SetValue(this.TlsTrma.Name, numArray[4], r);
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }

        public override void SetParam(string[] fields, string[] source, double[] periods, int panelID)
        {
            this.TlsTrix = base.Crab.AddLine(fields[0], panelID);
            base.Crab.SetLine(fields[0], fields[0], Color.White, 1, 0.01, 2, DashStyle.Solid);
            this.TlsTrma = base.Crab.AddLine(fields[1], panelID);
            base.SourceDic["Close"] = source[0];
            base.IndPeriod[0].ParamValue = periods[0];
            base.IndPeriod[1].ParamValue = periods[1];
            base.Crab.DataSource.Columns.Add(this.FirstEmaField);
            base.Crab.DataSource.Columns.Add(this.SecondEmaField);
            base.Crab.DataSource.Columns.Add(this.ThirdEmaField);
        }

        public string FirstEmaField
        {
            get
            {
                return this.firstEmaField;
            }
            set
            {
                this.firstEmaField = value;
            }
        }

        public string SecondEmaField
        {
            get
            {
                return this.secondEmaField;
            }
            set
            {
                this.secondEmaField = value;
            }
        }

        public string ThirdEmaField
        {
            get
            {
                return this.thirdEmaField;
            }
            set
            {
                this.thirdEmaField = value;
            }
        }

        public LineShape TlsTrix
        {
            get
            {
                return this.tlsTrix;
            }
            set
            {
                this.tlsTrix = value;
            }
        }

        public LineShape TlsTrma
        {
            get
            {
                return this.tlsTrma;
            }
            set
            {
                this.tlsTrma = value;
            }
        }
    }
}

